package ge.telasi.tasks.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * @author dimitri
 */
@Entity
@Table(name = "structures")
@NamedQueries({
    @NamedQuery(name = "Structure.selectRoot", query = "SELECT s FROM Structure s WHERE s.parent IS NULL ORDER BY s.name")
})
public class Structure extends Modifiable implements Serializable, Structurable<Structure> {
    private static final long serialVersionUID = -2071514794132034866L;

    public static final int DIRECTION = 1;
    public static final int DEPARTMENT = 2;
    public static final int DIVISION = 3;
    public static final int GROUP = 4;
    public static final int JOB = 5;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "STRUCTURES_SEQ")
	@SequenceGenerator(name = "STRUCTURES_SEQ", sequenceName = "STRUCTURES_SEQ", allocationSize = 1)
    private Integer id;
    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Structure parent;
    @Column(name = "type_id")
    private int type;
    @Column(name = "name")
    private String name;
    @Column(name = "name2")
    private String name2;
    @OneToMany(mappedBy = "parent")
    @OrderBy("name")
    private List<Structure> children = new ArrayList<Structure>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Structure getParent() {
        return parent;
    }

    public void setParent(Structure parent) {
        this.parent = parent;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    public List<Structure> getChildren() {
        return children;
    }

    public void setChildren(List<Structure> children) {
        this.children = children;
    }

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder();
        b.append(name);
        if (name2 != null && name2.trim().length() > 0) {
            b.append(" / ");
            b.append(name2);
        }
        return b.toString();
    }

    @Override
    public int hashCode() {
        return id == null ? 0 : id.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Structure other = (Structure) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

}
